package com.jdbc.pet;

import com.jdbc.pet.Pet;

import java.util.List;
import java.util.Scanner;

public class JDBCDemo {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("欢迎来到宠物商店管理系统");

        User user = login(scanner);
        if (user != null) {
            if (user.isAdmin()) {
                adminMenu(scanner);
            } else {
                customerMenu(scanner, user);
            }
        } else {
            System.out.println("登录失败，请检查用户名和密码！");
        }

        scanner.close();
    }

    private static User login(Scanner scanner) {
        System.out.print("请输入用户名: ");
        String username = scanner.nextLine();
        System.out.print("请输入密码: ");
        String password = scanner.nextLine();

        String sql = "SELECT * FROM users WHERE username = ? AND password = ?";
        List<User> users = JDBCUtils.query(User.class, sql, username, password);
        if (!users.isEmpty()) {
            return users.get(0);
        }
        return null;
    }

    private static void adminMenu(Scanner scanner) {
        while (true) {
            System.out.println("\n管理员菜单:");
            System.out.println("1. 添加宠物");
            System.out.println("2. 更新宠物");
            System.out.println("3. 删除宠物");
            System.out.println("4. 查看宠物");
            System.out.println("5. 退出");
            System.out.print("请选择操作: ");
            int choice = scanner.nextInt();
            scanner.nextLine(); // 清除缓冲区

            switch (choice) {
                case 1:
                    addPet(scanner);
                    break;
                case 2:
                    updatePet(scanner);
                    break;
                case 3:
                    deletePet(scanner);
                    break;
                case 4:
                    viewPets(scanner);
                    break;
                case 5:
                    return;
                default:
                    System.out.println("无效的选择，请重新选择！");
            }
        }
    }

    private static void customerMenu(Scanner scanner, User user) {
        while (true) {
            System.out.println("\n顾客菜单:");
            System.out.println("1. 查看宠物");
            System.out.println("2. 购买宠物");
            System.out.println("3. 退出");
            System.out.print("请选择操作: ");
            int choice = scanner.nextInt();
            scanner.nextLine(); // 清除缓冲区

            switch (choice) {
                case 1:
                    viewPets(scanner);
                    break;
                case 2:
                    buyPet(scanner, user);
                    break;
                case 3:
                    return;
                default:
                    System.out.println("无效的选择，请重新选择！");
            }
        }
    }

    private static void addPet(Scanner scanner) {
        System.out.print("请输入宠物昵称: ");
        String nickname = scanner.nextLine();
        System.out.print("请输入宠物类型: ");
        String type = scanner.nextLine();
        System.out.print("请输入宠物年龄: ");
        int age = scanner.nextInt();
        scanner.nextLine(); // 清除缓冲区
        System.out.print("请输入宠物性别: ");
        String gender = scanner.nextLine();
        System.out.print("请输入宠物价格: ");
        int price = scanner.nextInt();
        scanner.nextLine(); // 清除缓冲区

        String sql = "INSERT INTO pets (nickname, type, age, gender, price) VALUES (?, ?, ?, ?, ?)";
        int rows = JDBCUtils.update(sql, nickname, type, age, gender, price);
        if (rows > 0) {
            System.out.println("宠物添加成功！");
        } else {
            System.out.println("宠物添加失败！");
        }
    }

    private static void updatePet(Scanner scanner) {
        System.out.print("请输入要更新的宠物ID: ");
        int id = scanner.nextInt();
        scanner.nextLine(); // 清除缓冲区
        System.out.print("请输入新的宠物昵称: ");
        String nickname = scanner.nextLine();
        System.out.print("请输入新的宠物类型: ");
        String type = scanner.nextLine();
        System.out.print("请输入新的宠物年龄: ");
        int age = scanner.nextInt();
        scanner.nextLine(); // 清除缓冲区
        System.out.print("请输入新的宠物性别: ");
        String gender = scanner.nextLine();
        System.out.print("请输入新的宠物价格: ");
        int price = scanner.nextInt();
        scanner.nextLine(); // 清除缓冲区

        String sql = "UPDATE pets SET nickname = ?, type = ?, age = ?, gender = ?, price = ? WHERE id = ?";
        int rows = JDBCUtils.update(sql, nickname, type, age, gender, price, id);
        if (rows > 0) {
            System.out.println("宠物更新成功！");
        } else {
            System.out.println("宠物更新失败！");
        }
    }

    private static void deletePet(Scanner scanner) {
        System.out.print("请输入要删除的宠物ID: ");
        int id = scanner.nextInt();
        scanner.nextLine(); // 清除缓冲区

        String sql = "DELETE FROM pets WHERE id = ?";
        int rows = JDBCUtils.update(sql, id);
        if (rows > 0) {
            System.out.println("宠物删除成功！");
        } else {
            System.out.println("宠物删除失败！");
        }
    }

    private static void viewPets(Scanner scanner) {
        System.out.println("1. 按价格排序");
        System.out.println("2. 按年龄排序");
        System.out.print("请选择排序方式: ");
        int choice = scanner.nextInt();
        scanner.nextLine(); // 清除缓冲区

        String sql;
        switch (choice) {
            case 1:
                sql = "SELECT * FROM pets ORDER BY price";
                break;
            case 2:
                sql = "SELECT * FROM pets ORDER BY age";
                break;
            default:
                System.out.println("无效的选择，请重新选择！");
                return;
        }

        List<Pet> pets = JDBCUtils.query(Pet.class, sql);
        for (Pet pet : pets) {
            System.out.println(pet);
        }
    }

    private static void buyPet(Scanner scanner, User user) {
        System.out.print("请输入要购买的宠物ID: ");
        int petId = scanner.nextInt();
        scanner.nextLine(); // 清除缓冲区

        String sql = "SELECT * FROM pets WHERE id = ?";
        List<Pet> pets = JDBCUtils.query(Pet.class, sql, petId);
        if (pets.isEmpty()) {
            System.out.println("宠物不存在！");
            return;
        }

        Pet pet = pets.get(0);
        if (user.getPoints() < pet.getPrice()) {
            System.out.println("积分不足，无法购买该宠物！");
            return;
        }

        // 更新宠物的主人ID
        String updatePetSql = "UPDATE pets SET ownerId = ? WHERE id = ?";
        int updateRows = JDBCUtils.update(updatePetSql, user.getId(), petId);
        if (updateRows > 0) {
            // 扣除用户的积分
            user.setPoints(user.getPoints() - pet.getPrice());
            String updateUserSql = "UPDATE users SET points = ? WHERE id = ?";
            int updatePointsRows = JDBCUtils.update(updateUserSql, user.getPoints(), user.getId());
            if (updatePointsRows > 0) {
                System.out.println("宠物购买成功！");
            } else {
                System.out.println("积分扣除失败！");
            }
        } else {
            System.out.println("宠物购买失败！");
        }
    }
}
